package com.bsj.travel.def.monitor.tdengine;

import com.bsj.travel.enums.DataTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author yinhao
 * @version 1.0
 * @description TD建表的字段实体
 * @date 2024/1/31
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Fields implements Serializable {
    private static final long serialVersionUID = -640479996287472589L;
    /**
     * 字段名称
     */
    private String fieldName;
    /**
     * 字段值
     */
    private Object fieldValue;
    /**
     * 字段数据类型
     */
    private DataTypeEnum dataType;
    /**
     * 字段字节大小
     */
    private Integer size;

    public Fields(String fieldName, String dataType, Integer size) {
        this.fieldName = fieldName;
        //根据规则匹配字段数据类型
        switch (dataType.toLowerCase()) {
            case ("json"):
                this.dataType = DataTypeEnum.JSON;
                this.size = size;
                break;
            case ("string"):
                this.dataType = DataTypeEnum.NCHAR;
                this.size = size;
                break;
            case ("varchar"):
                this.dataType = DataTypeEnum.VARCHAR;
                this.size = size;
                break;
            case ("binary"):
                this.dataType = DataTypeEnum.BINARY;
                this.size = size;
                break;
            case ("int"):
                this.dataType = DataTypeEnum.INT;
                break;
            case ("bool"):
                this.dataType = DataTypeEnum.BOOL;
                break;
            case ("decimal"):
                this.dataType = DataTypeEnum.DOUBLE;
                break;
            case ("timestamp"):
                if ("eventTime".equals(fieldName)) {
                    this.fieldName = "eventTime";
                }
                this.dataType = DataTypeEnum.TIMESTAMP;
                break;
        }
    }

    public Fields(String fieldName, String dataType, Integer size, Object fieldValue) {
        this.fieldName = fieldName;
        this.fieldValue = fieldValue;
        //根据规则匹配字段数据类型
        switch (dataType.toLowerCase()) {
            case ("json"):
                this.dataType = DataTypeEnum.JSON;
                this.size = size;
                break;
            case ("string"):
                this.dataType = DataTypeEnum.NCHAR;
                this.size = size;
                break;
            case ("binary"):
                this.dataType = DataTypeEnum.BINARY;
                this.size = size;
                break;
            case ("int"):
                this.dataType = DataTypeEnum.INT;
                break;
            case ("bool"):
                this.dataType = DataTypeEnum.BOOL;
                break;
            case ("decimal"):
                this.dataType = DataTypeEnum.DOUBLE;
                break;
            case ("timestamp"):
                if ("eventTime".equals(fieldName)) {
                    this.fieldName = "eventTime";
                }
                this.dataType = DataTypeEnum.TIMESTAMP;
                break;
        }
    }
}
